package com.smartscreen.baseinfomanage.service.impl;

import com.smartscreen.baseinfomanage.dto.CreateAcademicYearRequest;
import com.smartscreen.baseinfomanage.entity.AcademicYear;
import com.smartscreen.baseinfomanage.entity.Term;
import com.smartscreen.baseinfomanage.mapper.AcademicYearMapper;
import com.smartscreen.baseinfomanage.mapper.TermMapper;
import com.smartscreen.baseinfomanage.service.AcademicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class AcademicServiceImpl implements AcademicService {

    @Autowired
    private AcademicYearMapper academicYearMapper;
    
    @Autowired
    private TermMapper termMapper;

    @Override
    public List<AcademicYear> getAllAcademicYears() {
        // 获取所有学年
        List<AcademicYear> academicYears = academicYearMapper.selectAllWithOrder();
        
        // 为每个学年加载对应的学期
        for (AcademicYear academicYear : academicYears) {
            List<Term> terms = termMapper.selectByYearId(academicYear.getYearId());
            academicYear.setTerms(terms);
        }
        
        return academicYears;
    }

    @Override
    @Transactional
    public String createAcademicYear(CreateAcademicYearRequest request) {
        // 生成学年ID
        String yearId = generateYearId();
        
        // 创建学年对象
        AcademicYear academicYear = new AcademicYear();
        academicYear.setYearId(yearId);
        academicYear.setName(request.getName());
        
        // 保存学年
        academicYearMapper.insert(academicYear);
        
        // 保存学期
        if (request.getTerms() != null && !request.getTerms().isEmpty()) {
            for (int i = 0; i < request.getTerms().size(); i++) {
                Term term = request.getTerms().get(i);
                // 生成学期ID
                String termId = yearId + "0" + (i + 1);
                term.setTermId(termId);
                term.setYearId(yearId);
                
                // 保存学期
                termMapper.insert(term);
            }
        }
        
        return yearId;
    }
    
    /**
     * 生成学年ID
     * 格式：y + 年份
     */
    private String generateYearId() {
        // 获取当前最大的学年ID，生成新的ID
        List<AcademicYear> existingYears = academicYearMapper.selectList(null);
        
        if (existingYears.isEmpty()) {
            return "y2024";
        }
        
        // 找到最大的年份数字
        int maxYear = existingYears.stream()
                .mapToInt(year -> {
                    String yearId = year.getYearId();
                    if (yearId != null && yearId.startsWith("y") && yearId.length() > 1) {
                        try {
                            return Integer.parseInt(yearId.substring(1));
                        } catch (NumberFormatException e) {
                            return 2024; // 默认值
                        }
                    }
                    return 2024; // 默认值
                })
                .max()
                .orElse(2024);
        
        return "y" + (maxYear + 1);
    }
}